<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 项目数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\TemplateService;
use Exception;
use think\Request;
use think\Response;

class ProjectTemplateController extends VerifyController
{
    /**
     * @var TemplateService
     */
    private $projectTemplateService;

    public function __construct()
    {
        parent::__construct();
        $this->projectTemplateService = TemplateService::getInstance();
    }

    /**
     * 计划模板查询
     * @param Request $request
     * @return Response
     */
    public function findPlanTemplate(Request $request)
    {
        $param = $request->param();
        $res = $this->projectTemplateService->findPlanTemplate($param['param']);
        return json($res);
    }

    /**
     * 计划模板更新
     * @param Request $request
     * @return Response
     */
    public function updatePlanTemplate(Request $request)
    {
        $param = $request->param();
        $res = $this->projectTemplateService->updatePlanTemplate($param['data']);
        return json(success_response("", $res));
    }

    /**
     * 任务统计工序项目模板查询
     * @param Request $request
     * @return Response
     */
    public function findStatisticalStepTemplate(Request $request)
    {
        $param = $request->param();
        try {
            $res = $this->projectTemplateService->findStatisticalStepTemplate($param['param']);
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新任务统计工序项目模板配置
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function updateStatisticalStepTemplate(Request $request)
    {
        $param = $request->param();
        try {
            $res = $this->projectTemplateService->updateStatisticalStepTemplate($param['data']);
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查询模块字段配置
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function findModuleFieldConfig(Request $request)
    {
        $param = $request->param();
        $res = $this->projectTemplateService->findModuleFieldConfig($param['param']['filter']);
        return json(success_response("", $res));
    }

    /**
     * 更新模块字段配置
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function updateModuleFieldConfig(Request $request)
    {
        $param = $request->param();
        $res = $this->projectTemplateService->updateModuleFieldConfig($param['data']);
        return json(success_response("", $res));
    }

    /**
     * 项目模板环节配置查询
     * @param Request $request
     * @return Response
     */
    public function findStepCategoryConfig(Request $request)
    {
        $param = $request->param();
        $res = $this->projectTemplateService->findStepCategoryConfig($param['param']);
        return json(success_response("", $res));
    }

    /**
     * 更新任务统计工序项目模板配置
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function updateStepCategoryConfig(Request $request)
    {
        $param = $request->param();
        try {
            $res = $this->projectTemplateService->updateStepCategoryConfig($param['data']);
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}
